package me.helllp.dake.generator.engine;

import freemarker.template.TemplateException;
import lombok.Getter;
import lombok.Setter;
import me.helllp.dake.generator.config.GeneratorEngineConfiguration;
import me.helllp.dake.generator.config.ProjectConfiguration;
import me.helllp.dake.generator.engine.service.ApplicationGenerator;
import me.helllp.dake.generator.meta.PomMetaData;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Getter
@Setter
public class NormalServiceGenerator extends ProjectGenerator{
    public NormalServiceGenerator(ProjectConfiguration projectConfiguration) {
        super(projectConfiguration);
    }

    /**
     * 启动程序
     */
    private ApplicationGenerator applicationGenerator;

    @Override
    public void initGenerator(GeneratorEngineConfiguration engineConfiguration) throws TemplateException, IOException, InvocationTargetException, IllegalAccessException {
        super.initGenerator(engineConfiguration);
        this.getPomGenerator().addPlugin(new PomMetaData("org.springframework.boot", "spring-boot-maven-plugin", null));
        this.getPomGenerator().addDependency(makePoms());

        this.applicationGenerator = new ApplicationGenerator(getProjectMetaData());
        this.applicationGenerator.initGenerator(engineConfiguration);
    }

    @Override
    public void generator() throws IOException, TemplateException {
        // 创建基础路径
        super.generator();
        // 创建POM文件
        this.getPomGenerator().generator();
        // 创建配置文件
        this.getConfigurationGenerator().generator();
        // 生成启动文件
        this.getApplicationGenerator().generator();
        // 处理三方键配置
        Optional.ofNullable(this.getProjectMetaData().getThirdComponents()).orElse(Collections.emptyList()).forEach(third -> {
            AbstractGenerator thirdGenerator = null;
            try {
                thirdGenerator = third.toGenerator(this.getEngineConfiguration(), this.getProjectMetaData());
            } catch (TemplateException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            if (thirdGenerator != null) {
                try {
                    thirdGenerator.generator();
                } catch (IOException | TemplateException e) {
                    throw new RuntimeException(e);
                } catch (InvocationTargetException e) {
                    throw new RuntimeException(e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    private static List<PomMetaData> makePoms() {
        List<PomMetaData> list = new ArrayList<>();
        list.add(new PomMetaData("org.projectlombok", "lombok", null));
        list.add(new PomMetaData("org.springframework.boot", "spring-boot-starter-web", null));
        list.add(new PomMetaData("org.springframework.boot", "spring-boot-starter-test", null));
        list.add(new PomMetaData("org.springframework.boot", "spring-boot-starter-logging", null));
        list.add(new PomMetaData("junit", "junit", null));
        return list;
    }
}
